﻿@{
	Layout = "~/Views/Shared/_Layout.cshtml";
}

@using dPet.Application.DataObjects;
@using Hangerd.Utility;

@model StaffRoleDto

@helper CreatePrivilegesList(PrivilegesDto privileges)
{
	foreach (PrivilegesDto item in Enum.GetValues(typeof(PrivilegesDto)))
	{
		<label class="checkbox-inline">
			@if ((privileges & item) == item)
			{
				<input type="checkbox" value="@((int)item)" checked="checked" />
			}
			else
			{
				<input type="checkbox" value="@((int)item)" />
			}

			@CommonTools.GetEnumDescription(item)
		</label>
	}
}

<div class="clearfix">
	<h4>编辑角色信息</h4>

	<hr />

	@using (Html.BeginForm("SaveStaffRole", "Manage", FormMethod.Post, new { id = "save_staffrole_form" }))
	{
		<div class="panel panel-default">
			<div class="panel-heading">
				<button id="btn_cancel" type="button" class="btn btn-sm btn-default" onclick="window.history.go(-1);"><span class="glyphicon glyphicon-chevron-left"></span> 返 回</button>
				<button id="btn_submit" type="submit" class="btn btn-sm btn-success"><span class="glyphicon glyphicon-floppy-disk"></span> 保 存</button>
			</div>

			<div class="panel-body form-horizontal">
				@Html.HiddenFor(x => x.Id)
				<input id="privileges_hidden" name="Privileges" type="hidden" value="@((int)Model.Privileges)" />

				<div class="form-group">
					<label for="Name" class="col-md-1 control-label">角色名称</label>
					<div class="col-md-3">
						@Html.TextBoxFor(x => x.Name, new { @class = "form-control", placeholder = "请输入角色名称" })
					</div>
				</div>

				<div class="form-group">
					<label for="Name" class="col-md-1 control-label">权限列表</label>
					<div id="privilege_box" class="col-md-4">
						@CreatePrivilegesList(Model.Privileges)
					</div>
				</div>
			</div>
		</div>
	}
</div>

@section scripts {

	<script type="text/javascript">
		$("#btn_submit").bind("click", function () {
			if ($.trim($("#Name").val()) == '') {
				alert("请输入角色名称");
				return false;
			}

			var privileges = 0;

			$("#privilege_box input[type=checkbox]").each(function () {
				if ($(this).prop('checked')) {
					privileges |= $(this).val();
				}
			});

			$("#privileges_hidden").val(privileges);
			$("#save_staffrole_form").submit();
		});
	</script>
}
